import axios from '@/utils/request'
export default {
  Activity: {
    list: {
      query: {
        '@order': `entity.id|ASC`
      },
      list_filter: {
      },
      actions: [
        { 
          name: 'refund',
          position: 'list',
          component: {
            props: ['record', 'refresh'],
            data() {
              return {
                loading: false
              }
            },
            render(h) {
              return (
                <div>
                  <el-popconfirm title='执行之后报名费用将不能退还，是否确定执行？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.post(`/api/activities/${this.record.id}/settle`).then((res) => {
                            this.$message.success('执行成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='success' icon='el-icon-video-play' loading={this.loading} plain>执行活动</el-button>
                  </el-popconfirm>
                  <el-popconfirm title='批量退款会将已报名支付但是未结算的退回，是否确定操作？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.get(`/manage/activity-records`, {
                            params: {
                              '@filter': `entity.getStatus() == 1 && entity.getActivity().getId() == ${this.record.id}`
                            }
                          }).then((res) => {
                            console.log(res.data)
                            if (res.data.length > 0) {
                              let count = res.data.length
                              for (let i of res.data) {
                                this.axios.post(`manage/activity-records/${i.id}/refund`).then((res) => {
                                  count--
                                  if (count <= 0) {
                                    this.$message.success('操作成功，已全部退还')
                                    this.loading = false
                                  }
                                })
                              }
                            } else {
                              this.$message.error('该活动没有可退款记录！')
                              this.loading = false
                            }
                            // this.$message.success('执行成功！')
                            // this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='info' icon='el-icon-refresh-left' loading={this.loading} plain style='margin:0px 10px;'>批量退款</el-button>
                  </el-popconfirm>
                </div>

              )
            }
          }
        }
      ],
      list_display: [
        {
          property: 'title',
          label: '活动标题'
        },
        {
          property: 'settler',
          label: '收款账号'
        },
        {
          property: 'price',
          label: '报名费用'
        },
        {
          property: 'enabled',
          label: '开通报名',
          editAbled: true
        },
        {
          property: 'startTime',
          label: '活动开始时间'
        }
      ]
    },
    form: {
      fields: [
        'title',
        {
          property: 'image',
          type: 'image'
        },
        {
          property: 'settler',
          field_options: { label: '收款账号' },
          help: '活动开始时候，钱会扣除支付手续费之后，全部到该账号'
        },
        'price',
        {
          property: 'startTime',
          type: 'date'
        },
        { property: 'enabled', default_value: true },
        'description'
      ]
    }
  }
}

